package com.kitty.game.utils;

import java.util.Arrays;

public class BitSet {
    private long[] BV = new long[32];
    private Boolean[] bit = new Boolean[32];

    private void initiate() {
        for (int i = 1; i <= 32; i++) {
            BV[i - 1] = (long) Math.pow(2, i - 1);
        }
    }

    {
        initiate();
    }

    public static BitSet NEW(){
       return new BitSet(0);
    }
    public BitSet(long i32) {
        this.set(i32);
    }


    private void setI32(int startPos, long i32) {
        for (int j = 1; j <= 32; j++) {
            int i = 33 - j - 1;
            if (i32 >= BV[i]) {
                bit[startPos + i] = true;
                i32 = i32 - BV[i];
            } else {
                bit[startPos + i] = false;
            }
        }
    }

    public void set(long i32) {
        long[] nums = {i32};
        int startPos = 0;
        for (long num : nums) {
            setI32(startPos, num);
            startPos += 32;
        }
    }

    public void setBit(int index, Boolean value) {
//        index -= 1;
        if(index>=32){ /**只能有0-31 所以到了32就减32*/
            index-=32;
        }
        if (index < 0 || index > bit.length) {
            return;
        }
        bit[index] = value;
    }

    /**图标用 TODO 需要-1 java 跟lua位置不同*/
    public void setBit(int index) {
        index -= 1;
        if(index>=32){ /**只能有0-31 所以到了32就减32*/
            index-=32;
        }
        if (index < 0 || index > bit.length) {
            return;
        }
        bit[index] = true;
    }


    public boolean isSet(int index) {
        if (index < 0 || index > bit.length) {
            return false;
        }
        return bit[index];
    }

    public int getI32() {
        int i32 = 0;
        int i = 0;
        for (Boolean boo : this.bit) {
            if (boo != null) {
                if (boo) {
                    i32 += BV[i];
                }
            }
            i++;
        }
        return i32;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BitSet bitSet = (BitSet) o;
        return Arrays.equals(BV, bitSet.BV);
    }

}
